package com.efuture.androidmvvmdemo.view.adapter

import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.efuture.netrequestdemo.bean.DataS
import android.view.LayoutInflater
import android.view.View
import com.efuture.androidmvvmdemo.databinding.ItemHomedataBinding


/**

 *@description:TODO
 * @author: zhaoshanshan
 * @date: 2022/3/26 8:17 下午

 */
class HomeDataAdapter(var data: List<DataS>?) : RecyclerView.Adapter<HomeDataAdapter.ViewHolder>() {


    override fun getItemCount(): Int {
        return data?.size?:0
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {

        return  ViewHolder(
            ItemHomedataBinding.inflate(LayoutInflater.from(parent.getContext()),
            parent, false));

    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {

        holder.binding?.tvTitle?.text = data?.get(position)?.title
        holder.binding?.tvTitle?.setOnClickListener(View.OnClickListener {
            ToastUtils.showShort("点击了条目$position")
        })
    }

    inner class ViewHolder(b: ItemHomedataBinding) : RecyclerView.ViewHolder(b.root) {
        var binding : ItemHomedataBinding?=null

        init {
            binding = b
        }
    }

    fun updateList(list: List<DataS>) {
        data = list
        notifyDataSetChanged()
    }
}